package com.yfve.eol;


import android.car.hardware.CarPropertyValue;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import androidx.annotation.NonNull;

import com.yfve.eol.activity.TftDisplayPatternActivity;
import com.yfve.eol.activity.TouchTestActivity;
import com.yfve.eol.service.AgingTestService;
import com.yfve.eol.util.BackLightUtil;
import com.yfve.eol.util.Rl78Util;
import com.yfve.eol.util.ScreenEolUtil;

import java.util.Arrays;

import static android.car.hardware.CarYFDiagManager.ID_EOL_C4905TouchPoints_GET;
import static android.car.hardware.CarYFDiagManager.ID_EOL_C490DimmingSts_SET;
import static android.car.hardware.CarYFDiagManager.ID_EOL_C490DisSoftVer_GET;
import static android.car.hardware.CarYFDiagManager.ID_EOL_C490EnterExitAgingTest_SET;
import static android.car.hardware.CarYFDiagManager.ID_EOL_C490EnterExitTestMode_SET;
import static android.car.hardware.CarYFDiagManager.ID_EOL_C490LCDCtrlSts_SET;
import static android.car.hardware.CarYFDiagManager.ID_EOL_C490TPSts_GET;
import static android.car.hardware.CarYFDiagManager.ID_EOL_C490TPVer_GET;
import static android.car.hardware.CarYFDiagManager.ID_EOL_C490TestScreenGesture_GET;

public class EolHandler extends Handler {

    public static final int MSG_EOL = 99;
    private static final String TAG = "EolHandler";
    private static EolHandler sEolHandler;

    private EolHandler() {
        Rl78Util.getInstance();
        ScreenEolUtil.getInstance();
    }

    public static EolHandler getEolHandler() {
        if (sEolHandler == null) {
            sEolHandler = new EolHandler();
        }
        return sEolHandler;
    }

    @Override
    public void handleMessage(@NonNull Message msg) {
        if (msg.what != MSG_EOL) {
            Log.e(TAG, "handleMessage: msg id is not " + MSG_EOL);
            return;
        }

        CarPropertyValue carPropertyValue = (CarPropertyValue) msg.obj;
        int id = carPropertyValue.getPropertyId();
        byte[] values;
        if (carPropertyValue.getValue() instanceof byte[]) {
            values = (byte[]) carPropertyValue.getValue();
        } else {
            Log.w(TAG, "Eol only handle byte array data");
            return;
        }
        Log.i(TAG, "handle command id = " + id + ", byte values = " + Arrays.toString(values));
        switch (id) {
            case ID_EOL_C490LCDCtrlSts_SET:
                Log.i(TAG, "handleMessage: ID_EOL_C490LCDCtrlSts_SET");
                TftDisplayPatternActivity.handleSetLCDCtrlSts(ID_EOL_C490LCDCtrlSts_SET, values);
                break;
            case ID_EOL_C490DimmingSts_SET:
                Log.i(TAG, "handleMessage: ID_EOL_C490DimmingSts_SET");
                BackLightUtil.handleSetScreenIllumination(ID_EOL_C490DimmingSts_SET, values);
                break;
            case ID_EOL_C490TestScreenGesture_GET:
                Log.i(TAG, "handleMessage: ID_EOL_C490TestScreenGesture_GET");
                TouchTestActivity.handleGetGesture(id);
                break;
            case ID_EOL_C4905TouchPoints_GET:
                Log.i(TAG, "handleMessage: ID_EOL_C4905TouchPoints_GET");
                TouchTestActivity.handleGetMultiPoints(id);
                break;
            case ID_EOL_C490TPVer_GET:
                Log.i(TAG, "handleMessage: ID_EOL_C490TPVer_GET");
                ScreenEolUtil.getInstance().handleGetTpVer(id);
                break;
            case ID_EOL_C490TPSts_GET:
                Log.i(TAG, "handleMessage: ID_EOL_C490TPSts_GET");
                ScreenEolUtil.getInstance().handleGetTPSts(id);
                break;
            case ID_EOL_C490DisSoftVer_GET:
                Log.i(TAG, "handleMessage: ID_EOL_C490DisSoftVer_GET");
                ScreenEolUtil.getInstance().handleGetDisplaySoftwareVer(id);
                break;
            case ID_EOL_C490EnterExitTestMode_SET:
                Log.i(TAG, "handleMessage: ID_EOL_C490EnterExitTestMode_SET");
                TouchTestActivity.handleSetEnterExitTestMode(id, values);
                break;
            case ID_EOL_C490EnterExitAgingTest_SET:
                Log.i(TAG, "handleMessage: ID_EOL_C490EnterExitTestMode_SET");
                AgingTestService.handleSetAgingTestCmd(id, values);
                break;
            default:
                break;
        }
    }

}
